package com.example.utils;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;

public class DateUtils {
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    /** 服务器启动时间（只记一次） */
    private static final Date SERVER_START_DATE = getJVMLaunchTime();

    private static Date getJVMLaunchTime() {
        RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
        long startTime = rb.getStartTime();
        return new Date(startTime);
    }

    public static Date getServerStartDate() {
        return SERVER_START_DATE;
    }

    public static Date getNowDate() {
        return new Date();
    }

    /** 格式化日期 */
    public static String parseDateToStr(String pattern, Date date) {
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 计算两个时间间隔，返回 *天*小时*分*秒
     */
    public static String timeDistance(Date end, Date start) {
        long diff = end.getTime() - start.getTime();
        long day  = diff / (24 * 60 * 60 * 1000);
        long hour = (diff - day * 24 * 60 * 60 * 1000) / (60 * 60 * 1000);
        long min  = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000) / (60 * 1000);
        long sec  = diff / 1000 % 60;
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    }
}
